package message.types.stalker;

import java.io.IOException;

import message.Message;
import network.e2e.FrameReader;
import network.e2e.FrameWriter;
import stalker.Stalker;
import stalker.StalkerParser;

public abstract class StalkerMessage extends Message {
  
  private final Stalker credential_;
  
  /**
   * Creates a new login success acknowledgement for the corresponding id
   */
  public StalkerMessage(Stalker client, Stalker destination, Stalker credential) {
    super(client, destination);
    assert credential != null;
    credential_ = credential;
  }
  
  /**
   * Retrieves this message over the net
   * @throws IOException 
   */
  public StalkerMessage(Stalker client, Stalker destination, FrameReader reader) throws IOException {
    super(client, destination);
    assert reader != null;
    credential_ = StalkerParser.retrieve(reader);
  }
  
  public Stalker getCredential() {
    return credential_;
  }

  public boolean contentEquals(Object obj) {
    if (!(obj instanceof StalkerMessage)) {
      return false;
    }
    StalkerMessage that = (StalkerMessage) obj;
    return this.getClient().equals(that.getClient()) &&
           this.getDestination().equals(that.getDestination()) &&
           this.getCredential().equals(that.getCredential());
  }

  @Override
  public void send(FrameWriter writer) throws IOException {
    StalkerParser.send(credential_, writer);
  }
  
}
